using Avalonia.BiliBili.Libs.Window;
using Avalonia.BiliBili.Units;
using Avalonia.BiliBili.Views.ViewModels;
using Avalonia.BiliBili.Windows;
using Avalonia.Controls;
using Avalonia.Extensions.Controls;
using Avalonia.Extensions.Event;
using Avalonia.Interactivity;
using Bilibili.Model.Bangumi;
using PCLUntils.Objects;

namespace Avalonia.BiliBili.Views
{
    public partial class BangmiView : View
    {
        private BangmiViewModel ViewModel { get; }
        public BangmiView()
        {
            InitializeComponent();
            ViewModel = new BangmiViewModel();
        }
        public override void AfterInitializeComponent()
        {
            myFollow.ItemClick += GridView_ItemClick; ;
            myFollow.ItemsSource = ViewModel.Bangumis;
            jpBangmi.ItemClick += GridView_ItemClick;
            jpBangmi.ItemsSource = ViewModel.JpBangumis;
            cnBangmi.ItemClick += GridView_ItemClick;
            cnBangmi.ItemsSource = ViewModel.CnBangumis;
            RunOnUiThread(async () =>
            {
                DataContext = ViewModel;
                ViewModel.ViewCallback += ViewModel_ViewCallback;
                await ViewModel.LoadData();
            });
        }
        private void OnMoreClick(object? sender, RoutedEventArgs e)
        {
            try
            {
                if (sender is Button button)
                {
                    var type = button.CommandParameter.ToInt32();
                    switch (type)
                    {
                        case 0:
                            Navigater.OpenWindow<BangmiWindow>();
                            break;
                        default:
                            Navigater.OpenWindow<ComicTypeWindow>(type);
                            break;
                    }
                }
            }
            catch { }
        }
        private void GridView_ItemClick(object? sender, ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickItem is GridViewItem item)
                {
                    if (item.Content is MyBangumi bangumi)
                        Navigater.OpenWindow<BanInfoWindow>(bangumi.Season_Id);
                    else if (item.Content is BangumiHome home)
                        Navigater.OpenWindow<BanInfoWindow>(home.Season_Id);
                }
            }
            catch { }
        }
        private void ViewModel_ViewCallback(string name, dynamic dyObj)
        {
            try
            {
                switch (name)
                {
                    case "jp":
                    case "cn":
                        var type = name == "jp" ? 1 : 2;
                        Navigater.OpenWindow<ComicTypeWindow>(type);
                        break;
                    case "tl":
                        Navigater.OpenWindow<TimelineWindow>();
                        break;
                    case "idx":
                        Navigater.OpenWindow<IndexWindow>();
                        break;
                }
            }
            catch { }
        }
    }
}